<?php
// +----------------------------------------------------------------------
// | Siteplus [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://www.brsttech.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Pillar <service@brsttech.com>
// +----------------------------------------------------------------------
namespace app\admin\model;
use think\Model;
use category\Category;

class Auth extends Model{

    protected $pk       = 'id';
    protected $table    = 'sp_system_auth';
    protected $autoWriteTimestamp = true;
    protected $pageNumber = 20;

    /**
     * 单个
     * @param int $param
     * @return array
     */
    public function find_info($param) {
        return self::get($param);
    }

    /**
     * 列表
     * @access public
     * @param array $where
     */
    public function select_lists($where = ['parent_id'=>0]){
        return $this->where($where)->order('id desc')->paginate($this->pageNumber);
    }

    /**
     * 权限列表
     * @access public
     * @return array
     */
    public function select_Lists_ids($id){
        $auth_array = [];
        if($id){
            $info = model('Group')->find_info($id);
            $auth_array = json_decode($info['auth_ids'],true);
        }
        $result = $this->field('id,title as name,parent_id as pId,auth_code')->order('id desc')->select();
        if($result){
            foreach ($result as $key => $value) {
                foreach ($auth_array as $id) {
                    if($id == $value['id']){
                        $result[$key] = ['id'=>$value['id'],'pId'=>$value['pId'],'name'=>$value['name'],'checked'=>true];
                    }
                }
            }   
        }
        return $result;
    }
  
    /**
     * 添加
     * @param array $param
     * @return int
     */
    public function add_data($param) {
        if($param['parent_id']){
            $info = $this->find_info($param['parent_id']);
            $param['root_id'] = $info->getAttr('root_id');
            $param['auth_node'] = $info->getAttr('auth_code').'.'.$param['auth_code'];
        }else{
            $param['root_id'] = 0;
            $param['auth_node'] = $param['auth_code'];
        }
        $data = [
            'parent_id'      => $param['parent_id'],
            'title'          => $param['title'],
            'root_id'        => $param['root_id'],
            'auth_code'      => $param['auth_node'],
            'auth_code_name' => $param['auth_code'],
            'create_time'    => time(),
            'update_time'    => time(),
        ];
        return $this->insertGetId($data);
    }
    
    /**
     * 编辑
     * @param type $param
     */
    public function edit_data($param) {
        if($param['parent_id']){
            $info = $this->find_info($param['parent_id']);
            $param['auth_node'] = $info->getAttr('auth_code').'.'.$param['auth_code']; 
        }else{
            $param['auth_node'] = $param['auth_code'];
        }
        $data = [
            'title'          => $param['title'],
            'auth_code'      => $param['auth_node'],
            'auth_code_name' => $param['auth_code'],
            'update_time'    => time(),
        ];
        $where = ['id'=>$param['id']];
        return $this->where($where)->update($data);
    }
    
    /**
     * 删除数据
     * @param int $id
     */
    public function delete_data($id) {
        return $this->destroy($id);
    }
    
    /**
     * 获取路径
     * @param int $parent_id
     */
    public function select_path($parent_id) {
        $pathMaps[] = [
            'name'=>'<i class="iconfont icon-home"></i> '.lang('lang_auth_ui_title'),'url'=>url('admin/auth/index')
        ];
        $result = $this->aute_node($parent_id);
        foreach ($result as $value) {
            $pathMaps[] = [
                'name'   =>'<i class="iconfont icon-arrowright"></i>'.$value['title'],
                'url'    => url('admin/auth/index',['parent_id'=>$value['id']]),
            ];
        }
        return $pathMaps;
    }
    
    /**
     * 获取当前路径
     * @param type $parent_id
     * @return type
     */
    public function aute_node($parent_id){
        $result = $this->field('id,title,parent_id')->select();
        $tree =  new Category(array('id','parent_id','title','name'));
        return $tree->getPath($result,$parent_id);
    }     
}